package net.introlab.scraper.stockcharts;

import net.introlab.scraper.BaseScraper;
import net.introlab.scraper.domain.Article;
import net.introlab.scraper.exception.ScraperException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class StockChartsScraper extends BaseScraper<Article> {

    public StockChartsScraper(Element element) {
        super(element);
    }

    @Override
    protected Article parseDocument(Document document) {
        Article article = new Article();
        article.setUrl(document.location());

        Elements title = document.select(".entry-header");
        article.setTitle(title.get(0).text());

        Elements text = document.select(".entry-body");
        article.setText(text.html());

        Elements images = document.select(".entry-body img");
        List<String> imagesList = new ArrayList<>();
        for (Element image : images) {
            imagesList.add(image.attr("src"));
        }
        article.setImages(imagesList);

        Elements date = document.select(".entry-date");
        article.setDate(date.get(0).text());

        Elements author = document.select(".by-author");
        String authorName = author.get(0).text().substring(11);
        article.setAuthor(authorName);

        return article;
    }

    @Override
    protected Document getDocument(Element element) {
        try {
            return Jsoup.connect(element.attr("href")).timeout(20000).get();
        } catch (IOException e) {
            throw new ScraperException(e);
        }
    }
}
